package com.yonyou.lxp.xutils3test.utils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by liuxiaopeng on 16/1/31.
 */
public class Tools {
    private static JSONObject jsonObject;

    /**
     * 判断 请求数据是否成功
     * @param result
     * @return
     */
    public static boolean isSuccess(String result) {
        boolean is = false;
        try {
            jsonObject = new JSONObject(result);
            if (jsonObject.has("resultCode")) {
                String code = jsonObject.getString("resultCode");
                is = "1".equals(code);
            } else if (jsonObject.has("resultcode") && "Successed!".equals(jsonObject.getString("reason"))) {
                String code = jsonObject.getString("resultcode");
                is = "200".equals(code);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return is;
    }

    /**
     * 获取jsonStr的某个字段  只限第一层
     *
     * @param result
     * @param key
     * @return
     */
    public static String getJsonStr(String result, String key) {
        String jsonStr = "";
        try {
            jsonObject = new JSONObject(result);
            if (jsonObject.has(key)) {
                jsonStr = jsonObject.getString(key);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonStr;
    }
}
